package com.gitee.wsl.compose.ui.grid

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import com.gitee.wsl.compose.ui.base.LazyGrid

class ColumnLayoutBean{
    var columnArray:ArrayList<ItemHeight> = ArrayList()
    var allHeight:Int = 0 //每一列的总高度
}
class ItemHeight{
    //原始下标
    var originIndex:Int = 0
    //child的高度
    var height:Int = 0
    //当前控件需要放置的Y轴位置
    var lastAllHeight:Int = 0
    //位于哪一列
    var column:Int = 0
}

@Composable
fun LazyGrid.VerticalStaggered(
    modifier: Modifier = Modifier,
    column: Int = 3,  // 自定义的参数，控制展示的行数，默认为 2列
    content: @Composable () -> Unit
) {
    Layout(
        modifier = modifier,
        content = content
    ) { measurables, constraints ->
        //Log.d("tgw", "CustomVerticalStaggeredGridmaxWidth:${constraints.maxWidth} ")
        //Log.d("tgw", "CustomVerticalStaggeredGridminWidth:${constraints.minWidth} ")
        //Log.d("tgw", "CustomVerticalStaggeredGrid maxHeight:${constraints.maxHeight} ")
        //Log.d("tgw", "CustomVerticalStaggeredGrid minHeight:${constraints.minHeight} ")
        val columnHeights = Array<ColumnLayoutBean>(column) { ColumnLayoutBean() }
        //存储所有的item项
        val columnArray: ArrayList<ItemHeight> = ArrayList<ItemHeight>()
        //存储每一列的高度总和
        val columnHeightArray = IntArray(column) { 0 }


        // 测量每个 child 尺寸，获得每个 child 的 Placeable 对象
        val placeables = measurables.mapIndexed { index, measurable ->

            // 标准流程：测量每个 child 尺寸，获得 placeable,约束宽度，最大为控件的宽度除以列
            val placeable = measurable.measure(constraints.copy(maxWidth = constraints.maxWidth / column))

            if (index < column) {//初始化列，先占满坑位
                columnHeights[index % column].allHeight = placeable.height

                val item = ItemHeight().apply {
                    this.originIndex = index
                    this.height = placeable.height
                    this.column = index % column
                    this.lastAllHeight = 0
                }
                columnHeights[index % column].columnArray.add(item)
                columnArray.add(item)
                columnHeightArray[index % column] = placeable.height

            }else {
                // 取出高度最小的列的下标，添加
                val minIndex = columnHeightArray.indices.minBy { columnHeightArray[it] }
                columnHeights[minIndex].allHeight += placeable.height
                columnHeightArray[minIndex] = columnHeights[minIndex].allHeight
                //获取当前item的开始时Y轴的放置位置
                var lastHeight = 0
                columnArray.filter { it.column == minIndex }.forEach {
                    lastHeight += it.height
                }
                val item = ItemHeight().apply {
                    this.originIndex = index
                    this.height = placeable.height
                    this.column = minIndex
                    this.lastAllHeight =  lastHeight
                }
                columnHeights[minIndex].columnArray.add(item)
                columnArray.add(item)
            }
            placeable    // 这句别忘了，返回每个 child 的placeable
        }

        // 取出最大列的高度
        val height = columnHeightArray.max()

        // 设置 自定义 Layout 的宽高
        layout(constraints.maxWidth, height) {
            // 摆放每个 child
            placeables.forEachIndexed { index, placeable ->
                val currentColumn = columnArray[index].column
                val x = (constraints.maxWidth / column * currentColumn).toInt()

                placeable.placeRelative(
                    x,
                    columnArray[index].lastAllHeight
                )
            }
        }
    }
}
